package cn.demomaster.quicktreeview;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class TreeViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<TreeNode> mAllNodes;
    List<TreeNode> mNodes;
    public Context mContext;
    /**
     * 默认不展开
     */
    private int defaultExpandLevel = 0;

    public TreeViewAdapter(Context context, List<TreeNode> dataList, int defaultExpandLevel,String focusedNodeId) {
        this.mContext = context;
        this.mAllNodes = dataList;
        this.defaultExpandLevel = defaultExpandLevel;
        List<TreeNode> nodeList = TreeData.filterExpandLevel(mAllNodes, defaultExpandLevel);
        this.mNodes = new ArrayList<>(TreeData.filterVisibleNode(nodeList));
        this.focusedNodeId = focusedNodeId;
    }
    public void setData(List<TreeNode> dataList){
        this.mAllNodes = dataList;
        List<TreeNode> nodeList = TreeData.filterExpandLevel(mAllNodes, defaultExpandLevel);
        this.mNodes = new ArrayList<>(TreeData.filterVisibleNode(nodeList));
        notifyDataSetChanged();
    }
    public String focusedNodeId;
    public void setFocusedNodeId(String focusedNodeId) {
        this.focusedNodeId = focusedNodeId;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TreeNode node = mNodes.get(position);
        // 设置内边距
        holder.itemView.setPadding(node.getLevel() * 30, holder.itemView.getPaddingTop(), holder.itemView.getPaddingRight(), holder.itemView.getPaddingBottom());
        // 设置节点点击时，可以展开以及关闭,将事件继续往外公布
        holder.itemView.setOnClickListener(v -> {
            setSelected2(position, true);
            setFocused(position);
            expandOrCollapse2(position);
            if (onNodeClickListener != null) {
                onNodeClickListener.onClick(holder,v,mNodes.get(position),
                        position);
            }
            notifyDataSetChanged();// 刷新视图
        });

        holder.itemView.setSelected(node.isSelected());
        onBindViewHolder(node, holder, position);
    }

    /**
     * 点击的回调接口
     */
    private OnTreeNodeClickListener onNodeClickListener;

    public void setOnTreeNodeClickListener(
            OnTreeNodeClickListener onTreeNodeClickListener) {
        this.onNodeClickListener = onTreeNodeClickListener;
    }

    /**
     * 相应ListView的点击事件 展开或关闭某节点
     *
     * @param position
     */
    public void expandOrCollapse(int position) {
        TreeNode n = mNodes.get(position);
        if (n != null) {// 排除传入参数错误异常
            if (!n.isLeaf()) {
                n.setExpand(!n.isExpand());
                mNodes = TreeData.filterVisibleNode(mAllNodes);
                notifyDataSetChanged();// 刷新视图
            }
        }
    }
    public void expandOrCollapse2(int position) {
        TreeNode n = mNodes.get(position);
        if (n != null) {// 排除传入参数错误异常
            if (!n.isLeaf()) {
                n.setExpand(!n.isExpand());
                mNodes = TreeData.filterVisibleNode(mAllNodes);
            }
        }
    }
    public void setChecked(int position, boolean checked) {
        TreeNode n = mNodes.get(position);
        if (n != null) {// 排除传入参数错误异常
            if (!n.isLeaf()) {
                n.setChecked(!n.isChecked());
                notifyDataSetChanged();// 刷新视图
            }
        }
    }

    public void setSelected(int position, boolean selected) {
        TreeNode n = mNodes.get(position);
        if (n != null) {// 排除传入参数错误异常
            if (!n.isLeaf()) {
                n.setSelected(!n.isSelected());
                notifyDataSetChanged();// 刷新视图
            }
        }
    }
    public void setSelected2(int position, boolean selected) {
        TreeNode n = mNodes.get(position);
        if (n != null) {// 排除传入参数错误异常
            if (!n.isLeaf()) {
                n.setSelected(!n.isSelected());
            }
        }
    }

    public void setFocused(int position) {
        for (TreeNode item : mNodes) {
            item.setFocused(false);
        }
        TreeNode n = mNodes.get(position);
        if (n != null) {// 排除传入参数错误异常
            n.setFocused(true);
        }
    }


    @Override
    public int getItemCount() {
        return mNodes.size();
    }

    public abstract void onBindViewHolder(TreeNode node, RecyclerView.ViewHolder holder, final int position);

    public interface OnTreeNodeClickListener {
        void onClick(RecyclerView.ViewHolder viewHolder, View view, TreeNode node, int position);
    }
}
